package com.motong.gongdan.config.dataSource;


//@Component
public class DataSourceConfig {


//    @Resource
//    private DatabaseListService databaseListService;
//
//    @Resource
//    private DataSource dataSource;
//
//    @Resource
//    private DynamicDataSourceAutoConfiguration dynamicConfig;
//
//    @Resource
//    private HikariDataSourceCreator hikariDataSourceCreator;
//
//    @PostConstruct
//    public void init() throws InterruptedException {
//        List<DatabaseList> databaseLists = databaseListService.list();
//        DynamicRoutingDataSource dynamicDataSource  = (DynamicRoutingDataSource) dataSource;
//        databaseLists.forEach(i->{
//            DataSourceProperty dataSourceProperty = new DataSourceProperty();
//            dataSourceProperty.setPoolName(i.getFactoryCode()).setLazy(true)
//                    .setDriverClassName("com.mysql.cj.jdbc.Driver").setUrl(i.getUrl())
//                    .setUsername(i.getUser()).setPassword(i.getPassword());
//
//            DataSource dataSource = hikariDataSourceCreator.createDataSource(dataSourceProperty);
//            dynamicDataSource.addDataSource(dataSourceProperty.getPoolName(),dataSource);
//        });
//    }
//
//    /**
//     * 切换数据源
//     *
//     * @param dataSourceName 数据源名称
//     *
//     * */
//    public void switchDataSource (String dataSourceName){
//        DataSource dataSource = dynamicConfig.dataSource();
//        if (dataSource instanceof DynamicRoutingDataSource){
//            DynamicRoutingDataSource dyDataSource = (DynamicRoutingDataSource) dataSource;
//            dyDataSource.setPrimary(dataSourceName);
//        }
//    }

}
